'use strict';

const Service = require('egg').Service;

class MemberService extends Service {
  // 处理用户购买礼包的情况，该处参数为直接邀请者
  async index(inviter, purchaseUser) {
    console.log(
      '处理用户购买礼包的情况，该处参数为直接邀请者 inviter',
      inviter
    );
    console.log(
      '购买者信息 purchaseUser',
      purchaseUser
    );
    const inviterClassifications = [
      {
        role: 'member',
        type: 'member', // 会员
        award: 10000,
      },
      {
        role: 'higher',
        type: 'directHigher', // 直接高级店主
        award: 5000,
      },
      {
        role: 'higher',
        type: 'indirectHigher', // 间接高级店主
        award: 2000,
      },
      {
        role: 'facilitator',
        type: 'facilitator', // 服务商
        award: 3000,
      },
    ];
    const compareUserWithinviterClassifications = (
      user,
      groupCommissionClassification
    ) =>
      groupCommissionClassification.role === user.role &&
      !groupCommissionClassification.rewarded;
    const newInviterClassificationsReduce = (
      total,
      currentValue,
      currentIndex
    ) => {
      console.log('MemberService newInviterClassificationsReduce total', total);
      console.log(
        'MemberService newInviterClassificationsReduce currentValue',
        currentValue
      );
      console.log(
        'MemberService newInviterClassificationsReduce currentIndex',
        currentIndex
      );
      if (currentValue.rewarded) return total;
      console.log(
        'MemberService newInviterClassificationsReduce inviter.role',
        inviter.role
      );
      console.log(
        'MemberService newInviterClassificationsReduce currentValue.role',
        currentValue.role
      );
      if (
        inviter.role === 'facilitator' &&
        currentValue.type === 'indirectHigher'
      ) {
        return total;
      }
      inviterClassifications[currentIndex].rewarded = true;
      return total + currentValue.award;
    };
    while (inviter) {
      // 找出当前邀请者在邀请者列表内的索引
      const inviterClassificationsIndex = inviterClassifications.findIndex(
        compareUserWithinviterClassifications.bind(this, inviter)
      );
      // 分离邀请者分类
      const newInviterClassifications = inviterClassifications.slice(
        0,
        inviterClassificationsIndex + 1
      );
      // 计算奖励
      const award = newInviterClassifications.reduce(
        newInviterClassificationsReduce,
        0
      );
      console.log('MemberService this.reward award', award);
      await this.reward(inviter, award, purchaseUser);
      inviter = await inviter.getInviter();
    }
  }
  // 奖励用户
  async reward(user, award, purchaseUser) {
    console.log('MemberService reward user.id', user.id);
    console.log('MemberService reward award', award);
    const newBalance = Number(user.balance) + award;
    console.log('MemberService reward newBalance', newBalance);
    const data = {
      userId: user.id,
      balance: newBalance,
      price: award,
      add: true,
      remark: `${purchaseUser.nickname}购买奖励`,
    };
    await this.app.model.BalanceRecord.create(data);
    await user.update({ balance: newBalance });
    // 找到受奖励用户号码
    const currentPhone = await this.app.model.Phone.findOne({
      where: {
        userId: user.id,
      },
      raw: true,
    });
    // 会员奖励
    const sendData = {
      phone: currentPhone.number,
      nickname: purchaseUser.nickname,
      TemplateCode: 'SMS_158547462',
    };
    await this.ctx.service.smsCode.send(sendData);
  }
}

module.exports = MemberService;
